// pages/chooseDate/chooseDate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curYear: 2019,
    curMonth: 9,
    days: [],
    popBgShow: false
  },
  onLoad() {
    let date = new Date();
    let curYear = date.getFullYear();
    let curMonth = date.getMonth() + 1;
    let curDay = date.getDay();
    this.calculateEmptyGrids(curYear, curMonth);
    this.calculateDays(curYear, curMonth);
    this.calculateNextDays(curYear, curMonth)
    this.setData({
      curYear,
      curMonth,
    });
  },
  //获取某年某月的天数
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  //获取某年某月的第一天星期
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  //渲染上一月日期
  calculateEmptyGrids(year, month) {
    let days = this.data.days;
    let lastMonthDays = this.getThisMonthDays(year, month - 1);
    let firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    for (let i = firstDayOfWeek; i > 0; i--) {
      days.push({
        id: month + '-' + (lastMonthDays - i + 1),
        day: lastMonthDays - i + 1,
        choosed: false,
        canOrder: false,
        isNow: false
      });
    }
    this.setData({
      days
    });
  },
  //渲染当月日期
  calculateDays(year, month) {
    let days = this.data.days;
    let firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let thisMonthDays = this.getThisMonthDays(year, month);
    for (let i = 1; i <= thisMonthDays; i++) {
      days.push({
        id: month + '-' + i,
        day: i,
        choosed: false,
        canOrder: true,
        isNow: true
      });
    }
    this.setData({
      days
    });
  },
  //渲染下月日期
  calculateNextDays(year, month) {
    let days = this.data.days;
    let firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let thisMonthDays = this.getThisMonthDays(year, month);
    let total = firstDayOfWeek + thisMonthDays;
    let nextMonthNum = (7 - total % 7) % 7;
    for (let i = 1; i <= nextMonthNum; i++) {
      days.push({
        id: (month + 1) + '-' + i,
        day: i,
        choosed: false,
        canOrder: false,
        isNow: false
      });
    }
    this.setData({
      days
    });
  },
  //日期点击事件
  orderDayTap: function(e) {
    let id = e.currentTarget.id,
      list = this.data.days;
    for (let i = 0; i < list.length; i++) {
      if (id == list[i].id) {
        if (!list[i].canOrder) return;
        list[i].onthis = true;
      } else {
        list[i].onthis = false;
      }
    }
    this.setData({
      days: list
    });
  },
  //上个月点击事件
  prevMonth: function(e) {
    let year = this.data.curYear;
    let month = this.data.curMonth;
    if (month == 1) {
      month = 12;
      year--;
    } else {
      month--;
    }
    this.setData({
      curMonth: month,
      curYear: year,
      days: []
    })
    this.calculateEmptyGrids(year, month);
    this.calculateDays(year, month);
    this.calculateNextDays(year, month);
  },
  //下个月点击事件
  nextMonth: function(e) {
    let year = this.data.curYear;
    let month = this.data.curMonth;
    if (month == 12) {
      month = 1;
      year++;
    } else {
      month++;
    }
    this.setData({
      curMonth: month,
      curYear: year,
      days: []
    })
    this.calculateEmptyGrids(year, month);
    this.calculateDays(year, month);
    this.calculateNextDays(year, month);
  },
  //确认点击事件
  confirmOrder: function(e) {
    this.setData({
      popBgShow: true,
    })
  },
  // 关闭弹层点击事件
  closePop: function (e) {
    console.log('弹层关闭！');
    this.setData({
      popBgShow: false,
    })
  },
  // 弹层确认点击事件
  popConfirmOrder: function (e){
    console.log('弹层点击确认！');
    this.setData({
      popBgShow: false,
    })
  }
})